<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery1">
        <a-row :gutter="24">
          <a-col :xl="4" :lg="7" :md="8" :sm="24">
            <a-form-item label="门店">
              <a-cascader
                :options="departModel.departTreeOptions"
                :showSearch="true"
                expand-trigger="hover"
                placeholder=""
                :fieldNames="departModel.fieldNames"
                style="width: 210px"
                v-model="queryParam.selecteddeparts"
              />
            </a-form-item>
          </a-col>
          <a-col :xl="5" :lg="7" :md="8" :sm="24">
            <a-form-item>
              <template #label>
                <a-select v-model="queryParam.dateColumn">
                  <a-select-option value="bizDate">申请时间</a-select-option>
                  <a-select-option value="auditDate">审核时间</a-select-option>
                </a-select>
              </template>
              <a-range-picker
                :ranges="{ 今天: [moment(), moment()], 近7天:[moment().subtract(7, 'days'),moment()],本月: [moment().startOf('month'), moment().endOf('month')] }"
                v-model="orderDateRange"
                @change="handleChange"
              />
            </a-form-item>
          </a-col>
          <a-col :xl="4" :lg="7" :md="8" :sm="24">
            <a-form-item label="单号">
              <a-input v-model="queryParam.billNo" allowClear></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="4" :lg="7" :md="8" :sm="24">
            <a-form-item label="状态">
              <a-select v-model="queryParam.bizStatus1" allowClear>
                <a-select-option value="'audit_pass'"> 通过 </a-select-option>
                <a-select-option value="'audit_refuse'"> 不通过 </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery1" icon="search">查询</a-button>
              <a-button type="default" @click="searchReset1" icon="reload" style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button type="primary" icon="download" @click="handleExportXls1('盘点单')" :loading="exportLoading">导出</a-button>
    </div>
    <!-- table区域-begin -->
    <div>
      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        class="j-table-force-nowrap"
        @change="handleTableChange"
      >
        <!-- ：new新建 approve申请 audit_pass通过 audit_refuse不通过 invalid作废 -->
        <span slot="bizStatus" slot-scope="text, record">
          <a-tag color="red" v-if="text == 'approve'">待审核</a-tag>
          <a-tag color="blue" v-if="text == 'audit_pass'">通过</a-tag>
          <a-tag color="orange" v-if="text == 'audit_refuse'">不通过</a-tag>
        </span>
        <span slot="action" slot-scope="text, record">
          <a @click="handleDetail(record)">详情</a>
        </span>
      </a-table>
    </div>
    <!-- table区域-end -->

    <!-- 表单区域 -->
    <posStockCheck-modal ref="modalForm" @ok="modalFormOk"></posStockCheck-modal>
  </a-card>
</template>

<script>
import '@/assets/less/TableExpand.less'
import PosStockCheckModal from './modules/PosStockCheckModal'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { queryDepartTreeList1 } from '@/api/api'
import moment from 'moment'

export default {
  name: 'PosStockCheckListQuery',
  mixins: [JeecgListMixin],
  components: {
    PosStockCheckModal,
  },
  data() {
    return {
      description: '盘点审核管理页面',
      disableMixinCreated: true,
      exportLoading: false,
      departModel: {
        departTreeOptions: [],
        fieldNames: { label: 'title', value: 'value', children: 'children' },
      },
      orderDateRange: [],
      moment,
      // 表头
      columns: [
        {
          title: '所属机构',
          align: 'center',
          dataIndex: 'orgName',
        },
        {
          title: '门店',
          align: 'center',
          dataIndex: 'shopName',
        },
        {
          title: '单号',
          align: 'center',
          dataIndex: 'billNo',
        },
        {
          title: '盘点类型',
          align: 'center',
          dataIndex: 'type',
          customRender: (text) => {
            return text == 'all' ? '全盘' : '部分盘点'
          },
        },
        {
          title: '经办人',
          align: 'center',
          dataIndex: 'bizUserName',
        },
        {
          title: '盘点时间',
          align: 'center',
          dataIndex: 'bizDate',
        },

        {
          title: '审核状态',
          align: 'center',
          dataIndex: 'bizStatus',
          scopedSlots: { customRender: 'bizStatus' },
        },
        {
          title: '审核时间',
          align: 'center',
          dataIndex: 'auditDate',
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          scopedSlots: { customRender: 'action' },
        },
      ],
      url: {
        list: '/stock/check/posStockCheck/list',
        exportXlsUrl: 'stock/check/posStockCheck/exportXls',
      },
    }
  },
  computed: {},
  created() {
    this.init()
  },
  methods: {
    init() {
      this.queryParam = {}
      this.queryParam.bizStatus = "'audit_pass','audit_refuse'"
      this.$set(this.queryParam,'dateColumn', 'bizDate')
      this.orderDateRange = []
      this.loadData(1)
      this.loadDepartTree()
    },
    searchReset1() {
      this.init()
    },
    assembleQueryParam() {
      console.log(this.queryParam.selecteddeparts)
      let selecteddeparts = this.queryParam.selecteddeparts
      if (selecteddeparts && selecteddeparts.length == 2) {
        this.queryParam.orgIds = selecteddeparts[0]
        this.queryParam.shopIds = selecteddeparts[1] == 0 ? null : selecteddeparts[1]
      } else {
        this.queryParam.orgIds = null
        this.queryParam.shopIds = null
      }
      let bizStatus1 = this.queryParam.bizStatus1
      if(bizStatus1){
        this.queryParam.bizStatus = bizStatus1
      }else{
        this.queryParam.bizStatus = "'audit_pass','audit_refuse'"
      }

    },
    searchQuery1() {
      this.assembleQueryParam()

      this.loadData(1)
    },
    handleChange(date, dateString) {
      this.queryParam.beginTime = dateString[0]
      this.queryParam.endTime = dateString[1]
    },
    loadDepartTree() {
      queryDepartTreeList1().then((res) => {
        if (res.success) {
          let departList = res.result
          if (departList.length == 1 && departList[0].orgType == 1) {
            departList = departList[0].children
          }
          this.departModel.departTreeOptions = departList

          this.departModel.departTreeOptions.forEach((x) => {
            if (!x['children']) {
              x['children'] = []
            }
            let allShop = { title: '全部门店', value: '0' }
            x['children'].unshift(allShop)
          })
        }
      })
    },
    handleDetail(record) {
      this.$refs.modalForm.view(record)
      this.$refs.modalForm.title = '盘点单详情'
      this.$refs.modalForm.disableSubmit = false
      this.$refs.modalForm.isView = true
    },
    async handleExportXls1(fileName) {
      this.assembleQueryParam()

      let selectOrgIds = this.queryParam.orgIds
      if(!selectOrgIds){
        this.$message.warning('请选择市场再导出')
        return false
      }


      this.exportLoading = true
      await this.handleExportXls(fileName)
      this.exportLoading = false
    },
  },
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>